package org.outlook.xnotepad.pojo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.outlook.xnotepad.command.action.CommandAction;
import org.outlook.xnotepad.command.action.SimpleCommandAction;

/**
 * function:
 * @author watson  email: watson.wu@hotmail.com
 * @date 2009-12-22
 */
public class Comman {
	private String wholeText;
	private String cmdText;
	private CommandAction cmdAct;
	private Pattern pattern = Pattern.compile("/exec:([^/]+?)/");

	public Comman(String text) throws Exception {
		this.wholeText = text;
		Matcher matcher = pattern.matcher(wholeText);
		if(matcher.find()) {
			cmdText = matcher.group(1);
		} else
			throw new Exception("regex not format");

		parser(cmdText);
	}

	private void parser(String text) throws Exception {
		if(StringUtils.isNotBlank(text)) {
			if(text.matches("\\[\\w*?-\\w*?\\](?:\\{\\w\\}){0,1}"))
				cmdAct = new SimpleCommandAction(text);
		}
	}

	public String getResult(int index) {
		return wholeText.replace("/exec:"+cmdText+"/", cmdAct.getValue(index));
	}

}